package cn.ware.house.interfaces.controller.inbountController;

import cn.ware.house.interfaces.entity.inventory.InboundOrder;
import cn.ware.house.interfaces.service.inboundservice.InboundOrderService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/inbound-orders")
public class InboundOrderController {

    private final InboundOrderService inboundOrderService;


    public InboundOrderController(InboundOrderService inboundOrderService) {
        this.inboundOrderService = inboundOrderService;
    }

    // 分页查询入库订单信息
    @GetMapping("/page")
    public ResponseEntity<List<InboundOrder>> getInboundOrdersByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        List<InboundOrder> inboundOrders = inboundOrderService.getInboundOrdersByPage(pageNum, pageSize);
        return new ResponseEntity<>(inboundOrders, HttpStatus.OK);
        // 接收前端传入的页码（默认值为1）和每页数量（默认值为10）参数，调用服务层方法进行分页查询，然后将查询结果返回给前端，状态码设置为200 OK
    }

    // 根据入库订单编号查询订单信息
    @GetMapping("/{inboundOrderId}")
    public ResponseEntity<InboundOrder> getInboundOrderById(@PathVariable Integer inboundOrderId) {
        InboundOrder inboundOrder = inboundOrderService.getInboundOrderById(inboundOrderId);
        if (inboundOrder!= null) {
            return new ResponseEntity<>(inboundOrder, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        // 根据前端传入的路径变量（入库订单编号），调用服务层方法查询对应的订单信息，如果查询到则返回给前端，状态码为200 OK，否则返回404 NOT_FOUND
    }

    // 插入入库订单信息
    @PostMapping
    public ResponseEntity<Boolean> addInboundOrder(@RequestBody InboundOrder inboundOrder) {
        System.out.println(inboundOrder);
        boolean result = inboundOrderService.addInboundOrder(inboundOrder);
        if (result) {
            return new ResponseEntity<>(true, HttpStatus.CREATED);
        } else {
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        // 接收前端以JSON格式传入的入库订单信息，调用服务层方法进行插入操作，根据操作结果返回相应的状态码和布尔值给前端，插入成功返回201 CREATED，失败返回500 INTERNAL_SERVER_ERROR
    }

    // 根据入库订单编号更新订单信息
    @PutMapping("/{inboundOrderId}")
    public ResponseEntity<Boolean> updateInboundOrderById(
            @PathVariable Integer inboundOrderId,
            @RequestBody InboundOrder inboundOrder) {
        inboundOrder.setInboundOrderId(inboundOrderId);
        boolean result = inboundOrderService.updateInboundOrderById(inboundOrder);
        if (result) {
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        // 接收前端传入的路径变量（入库订单编号）以及JSON格式的更新后的入库订单信息，设置订单编号后调用服务层方法进行更新操作，根据结果返回相应状态码和布尔值，更新成功返回200 OK，失败返回500 INTERNAL_SERVER_ERROR
    }

    // 根据入库订单编号删除订单信息
    @DeleteMapping("/{inboundOrderId}")
    public ResponseEntity<Boolean> deleteInboundOrderById(@PathVariable Integer inboundOrderId) {
        boolean result = inboundOrderService.deleteInboundOrderById(inboundOrderId);
        if (result) {
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        // 根据前端传入的路径变量（入库订单编号），调用服务层方法进行删除操作，根据结果返回相应状态码和布尔值，删除成功返回200 OK，失败返回500 INTERNAL_SERVER_ERROR
    }
}